package com.per.mybootall.jdk8Stream;

import com.alipay.api.domain.Person;
import com.google.common.collect.Lists;
import com.per.mybootall.pojo.UserPo;
import com.per.mybootall.pojo.up;
import io.swagger.models.auth.In;
import org.apache.commons.lang.time.DateUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collector;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.collectingAndThen;
import static java.util.stream.Collectors.toCollection;

/**
 * JDK1.8 新特性==》》stream流
 * */

public class Stream {
    public static void main(String[] args) throws ParseException {
        List<Integer> lis=new ArrayList<>();
        lis.add(1);   lis.add(8);   lis.add(6);   lis.add(4);   lis.add(2);   lis.add(5);   lis.add(6);   lis.add(9);   lis.add(7);   lis.add(10);   lis.add(11);
        System.out.println(lis);

        //distinct  去重    .sorted  正序排序      .sorted(Comparator.reverseOrder())  倒序排序   .collect(Collectors.toList())  将stream流重新转化为list   .count()求和
        lis.stream().distinct().sorted(Comparator.reverseOrder()).collect(Collectors.toList());
        System.out.println("对stream流去重与倒序排序后的数组为:"+lis.stream().distinct().sorted(Comparator.reverseOrder()).collect(Collectors.toList()));

        System.out.println( "去除stream流中为1的数据总和为："+lis.stream().filter(li->li!=1).count());//10

        System.out.println( "去除stream流中为1的数据list为："+lis.stream().filter(li->li!=1).collect(Collectors.toList()));

      List<Integer> arrayList=new ArrayList();
        System.out.print("经过stream流式计算得到的大于5的数据为： ");
        lis.stream().filter(li->{
            if(li>=5){
                System.out.print(li+" ; ");
                arrayList.add(li);
            }
            return false;
        }).collect(Collectors.toList());
        System.out.println();
        System.out.println("==>>>"+arrayList);

        //串行流
        System.out.println("串行流"+lis.stream().parallel().distinct().sorted().collect(Collectors.toList()));
        //并行流
        System.out.println("并行流" + lis.stream().sequential().distinct().sorted().collect(Collectors.toList()));
        System.out.println("=====================================================");



        /**
         * 上面是单独的针对integer的操作，下面是对对象的操作
         * */
        List<up> u=new ArrayList<>();
        up use=new up();
        use.setPassword(1);

        SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date loginTime = sdf.parse("2020-01-01 12:01:01");
        use.setUsername(loginTime);

        use.setPhotomessahes(1);
        u.add(use);
        for(int i=0;i<10;i++){
            if(i==1){
                up ue=new up();
                ue.setPassword(i);
                SimpleDateFormat sdf1 = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date loginTime1 = sdf.parse("2020-01-01 12:01:01");

                use.setUsername(loginTime1);
                ue.setPhotomessahes(i);
                u.add(ue);
            }else{
                up ue=new up();
                ue.setPassword(i);
                use.setUsername(new Date());
                ue.setPhotomessahes(i);
                u.add(ue);
            }

        }
        u.forEach(uuu->{
            System.out.println("初始list的数据为："+uuu);
        });

        List list1 = (List) u.stream().collect(
                Collectors.collectingAndThen(
                        Collectors.toCollection(
                                ()->new TreeSet<>(Comparator.comparing(up::getUsername))),
                        ArrayList::new));

        list1.forEach( e -> System.out.println("流式计算后的数据为："+e));


        List<up> aa = u.stream().distinct().collect(Collectors.toList());
        aa.forEach(uuuuu->{
            System.out.println("重写eq后的数据为："+uuuuu);
        });
    }


}
